package com.bruce.user.service.impl;

import com.bruce.pojo.User;
import com.bruce.rpc.RpcFactory;
import com.bruce.user.service.mapper.UserMapper;
import com.bruce.user.userService.UserService;
import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

/**
 * 服务对象，只要Spring去管理就好了
 * 只需要注册过程即可，调用自定义框架，把当前对象和实现的接口信息注册到zk中
 *
 * spring容器管理的bean对象，是调用构造方法创建的
 * @author Bruce
 */
@Service
public class UserServiceImpl extends UnicastRemoteObject implements UserService {
    public UserServiceImpl() throws RemoteException {
        System.out.println("实现类对象创建");
        try {
            RpcFactory.registerService(UserService.class, this);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
        System.out.println("注册服务");
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUserByName(String name) throws RemoteException {
        return userMapper.getUserByName(name);
    }
}
